package api

import (
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/utils"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	"path/filepath"
)

func PubWork(c *gin.Context) {
	var req request.PubWorkReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	file, _ := c.FormFile("image")

	ext := filepath.Ext(file.Filename)

	if ext != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"data": "上传视频格式错误",
		})
		return
	}

	if file.Size > 500*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"data": "上传视频超出500m,上传失败",
		})
		return
	}
	uploadFile, _, err := utils.MinioClient.UploadFile(file)
	if err != nil {
		return
	}

	utils.GetMinio("14.103.143.155:9000", "mujunhao", "hmy030830", "mjh", false)
	sms, _ := c1.PubWork(c, &__.PubWorkRequest{
		UserId:         int64(c.GetUint("userId")),
		WorksTitle:     req.WorksTitle,
		WorksMusicId:   int32(req.WorksMusicId),
		WorksImage:     uploadFile,
		WorksSubjectId: int32(req.WorksSubjectId),
		WorksType:      int64(req.WorksType),
		WorksExamine:   req.WorksExamine,
	})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"home": sms,
		},
	})
}

func WorksExamine(c *gin.Context) {
	var req request.WorksExamineReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	sms, _ := c1.WorksExamine(c, &__.WorksExamineRequest{
		WorkId:       int64(req.WorkId),
		WorksExamine: req.WorksExamine,
	})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"home": sms,
		},
	})
}
func SubjectList(c *gin.Context) {
	//var req request.SubjectListReq
	//	if err := c.ShouldBind(&req); err != nil {
	//		c.JSON(http.StatusOK, gin.H{
	//			"data": nil,
	//		})
	//		return
	//	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	sms, _ := c1.SubjectList(c, &__.SubjectListRequest{
		UserId: int64(c.GetUint("userId")),
	})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"home": sms,
		},
	})
}
func ExamineComment(c *gin.Context) {
	var req request.ExamineCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	sms, _ := c1.ExamineComment(c, &__.ExamineCommentRequest{
		Context: req.Context,
		Userid:  int64(c.GetUint("userId")),
		PadId:   int64(req.PadId),
	})

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"home": sms,
		},
	})
}
